﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class MmarkAreaAdd : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
            }
            if (!IsPostBack)
            {
                BindDdlODEP(ref ddlODEP);

                BindDdlChannel(ref ddlChannel);

                BinDdlProvince(ref ddlProvince);

                if (ddlChannel.Items.Count > 0 && ddlProvince.Items.Count > 0 && ddlODEP.Items.Count > 0)
                {
                    BindMaskArea();
                }
                else btnADD.Visible = false;
            }
        }
        #region DDL Bind
        private void BinDdlProvince(ref DropDownList ddl)
        {
            ddl.Items.Clear();
            DataTable dt = DbHelperSql.Query(string.Format("SELECT TOP 1 ChannelPro,ONO FROM T_Channel WITH(NOLOCK) WHERE FLAG='0' AND ChannelNO='{0}'", ddlChannel.SelectedValue)).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                string strPros = ";" + dt.Rows[0]["ChannelPro"].ToString();
                ddl.DataSource = BLSqlData.AllProvince.Where(e => strPros.Contains(";" + e.ProvinceID + ";")).ToList();
                ddl.DataValueField = "ProvinceID";
                ddl.DataTextField = "ProvinceName";
                ddl.DataBind();

                ddlODEP.SelectedValue = dt.Rows[0]["ONO"].ToString();
            }
        }
        private void BindDdlODEP(ref DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.DataSource = DbHelperSql.Query("SELECT ONO,ONO+'-'+ OName as Name FROM T_ODEP WITH(NOLOCK) ").Tables[0];
            ddl.DataValueField = "ONO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        private void BindDdlChannel(ref DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.DataSource = DbHelperSql.Query("SELECT ChannelNO,ChannelNO+'-'+ ChannelNumber+'-'+ChannelOrder as Name FROM T_Channel WITH(NOLOCK) WHERE FLAG='0' order by ChannelNO").Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
        #endregion

        private void BindMaskArea()
        {
            cblCity.Items.Clear();
            cblCity.DataSource = DbHelperSql.Query(string.Format("SELECT AreaNO,CityName FROM T_Area WITH(NOLOCK) WHERE ProvinceID={0} AND ONO='{1}'", ddlProvince.SelectedValue, ddlODEP.SelectedValue)).Tables[0];
            cblCity.DataTextField = "CityName";
            cblCity.DataValueField = "AreaNO";
            cblCity.DataBind();
        }

        protected void btnADD_Click(object sender, EventArgs e)
        {
            ArrayList alSQL = new ArrayList();
            alSQL.Add(string.Format("DELETE FROM T_MaskArea WHERE ChannelNO='{0}' AND ONO='{1}' AND ProvinceID={2}", ddlChannel.SelectedValue, ddlODEP.SelectedValue, ddlProvince.SelectedValue));
            bool bolFlag = false;
            for (int i = 0; i < cblCity.Items.Count; i++)
            {
                if (cblCity.Items[i].Selected)
                {
                    bolFlag = true;
                    alSQL.Add(string.Format("INSERT INTO T_MaskArea(ChannelNO,ONO,AreaNO,ProvinceID) VALUES('{0}','{1}','{2}',{3})", ddlChannel.SelectedValue, ddlODEP.SelectedValue, cblCity.Items[i].Value, ddlProvince.SelectedValue));
                }
            }
            if (bolFlag)
            {
                MM.DBUtility.DbHelperSql.ExecuteSqlTran(alSQL);
                MM.Common.Common.PageStartAlert(this.Page, "保存成功！");
                Server.Transfer("MmarkArea.aspx");
            }
            else
            {
                MM.Common.Common.PageStartAlert(this.Page, "请选择屏蔽城市！");
            }
        }

        protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
        {
            BinDdlProvince(ref ddlProvince);
            BindMaskArea();
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMaskArea();
        }
    }
}
